import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Vector;

/**
 * WORDTEST - JAVA Version
 * 
 *  This is a simple application aimed to help the user learn foreign languages.
 * 
 *  You can create word or phrases banks in a txt file using ISO-8859-1 codification (windows 
 *  standard), following a very simple format:
 *  
 *   word1 - meaning1
 *   word2 - meaning2
 *   ...
 * 
 *  The user banks will be loaded in memory and asked randomly to the user. 
 *  
 *  It allows the user mark words as difficult, for them to be asked insistently.
 * 
 * 
 * @author F. Javier R. Donado
 * @author fjrd84@gmail.com
 * @version 1.0.0
 */
public class WordTest {

	Vector<Word> words;
	Vector<Word> currentSet;
	Vector<Word> marked;
	FileReader fr;
	FileReader markedFile;
	String path;
	String markedPath = "marked.txt";
	boolean currentSetIsTheLast;
	int current;

	public WordTest() {
		process();
		path = "";
	}

	public static void main(String[] args) {
		new WordTest();
	}
	
	/**
	 * This is called when starting the application. It loads the difficult words
	 * and initializes the vectors.
	 */
	public void process() {
		try {
			fr = new FileReader("words.txt");
			markedFile = new FileReader("marked.txt");
		} catch (Exception e) {
			System.out.println("\nError en la lectura del archivo.\n");
		}
		this.currentSetIsTheLast = false;
		current = 0;
		words = new Vector<Word>();
		marked = new Vector<Word>();
		// Marked words are read.
		readData(markedPath, marked);
		words.addAll(marked);
		nextSet();
		// The window of the app is created and opened.
		new AWTApp(this);

	}

	public void nextSet() {
		currentSet = new Vector<Word>();
		int tmp;
		if (currentSetIsTheLast) {
			return;
		}
		if (words.size() >= 7) {
			for (int i = 1; i <= 7; i++) {
				tmp = (int) Math.floor(Math.random() * words.size());
				currentSet.add(words.get(tmp));
				words.remove(tmp);
			}
		} else {
			currentSetIsTheLast = true;
			while (words.size() > 0) {
				tmp = (int) Math.floor(Math.random() * words.size());
				currentSet.add(words.get(tmp));
				words.remove(tmp);
			}
		}
	}
	
	public void mark(){
		if(current>=currentSet.size()){
			return;
		}
		this.marked.add((Word)currentSet.get(current));
		if(!currentSetIsTheLast){
		this.words.add((Word)currentSet.get(current));
		}else{
			this.currentSet.add((Word)currentSet.get(current));
		}
		saveMarked();
	}
	
	public void unmark(){
		if(current>=currentSet.size()){
			return;
		}
		Word quitar = (Word)currentSet.get(current);
		this.marked.remove(quitar);	
		saveMarked();
	}

	public Word nextWord() {
		if (currentSet.size() > 0) {
			current = (current + 1) % currentSet.size();
			return (Word) currentSet.get(current);
		} else {
			return new Word("NO WORDS LEFT", " NO WORDS LEFT");
		}
	}
	
	public void saveMarked(){

		try {
			// BufferedReader br = new BufferedReader(fr);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(new File(markedPath)),
					Charset.forName("ISO-8859-1")));
			bw.flush();
			int i = 0;
			while(i<marked.size()){
				bw.write(marked.get(i).toString());
				bw.newLine();
				i++;
			}
			bw.close();

		} catch (Exception e) {
			System.out.println("Error de escritura: " + e.getMessage());
		}
	}

	public void readData(String filePath, Vector<Word> wordsList) {

		String meaning;
		String word;

		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(new File(filePath)),
					Charset.forName("ISO-8859-1")));
			String line = br.readLine();
			while (line != null) {
				word = line.substring(0, line.indexOf(" - "));
				meaning = line
						.substring(line.indexOf(" - ") + 3, line.length());
				wordsList.add(new Word(word, meaning));
				line = br.readLine();
				while (line != null && line.equals("")) {
					line = br.readLine();
				}
			}
			br.close();

		} catch (Exception e) {
			System.out.println("Reading error: " + e.getMessage());
		}
	}

	public void moreFiles(File file) {
		try {
			// System.out.println(file.getPath());
			this.fr = new FileReader(file.getPath());
			this.path = file.getPath();
			words = new Vector<Word>();
			this.readData(path, words);
			words.addAll(marked);
			this.currentSetIsTheLast = false;
			this.current = 0;
			this.nextSet();
		} catch (Exception e) {
			System.out.println("File error.");
		}
	}

	public void moreFiles(String file) {
		try {
			// System.out.println(file.getPath());
			this.fr = new FileReader(file);
			this.path = file;
			words = new Vector<Word>();
			this.readData(path, words);
			words.addAll(marked);
			this.currentSetIsTheLast = false;
			this.current = 0;
			this.nextSet();
		} catch (Exception e) {
			System.out.println("File error.");
		}
	}

	public int remainingWords() {
		return this.words.size() + this.currentSet.size();
	}
}
